package userbottle.controller;

import bottle.entity.Wxbottle;
import bottle.service.impl.Wxbottleserviceimpl;
import config.Interceptor.PassToken;

import details.service.impl.Userdetailsserviceimpl;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import userbottle.entity.Userbottle;
import userbottle.mapper.Userbottlemapper;
import userbottle.service.impl.Userbottleserviceimpl;
import utils.CommonRetumType;
import wx.entity.Wxuser;
import wx.mapper.WxuserMapper;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/userbottle")
public class Usertestcontroller {
    @Autowired
    Userbottlemapper userbottlemapper;

    @Autowired
    Userbottleserviceimpl userbottleserviceimpl;

    @Autowired
    private Wxbottleserviceimpl wxbottleserviceimpl;

    @Autowired
    private WxuserMapper wxuserMapper;


    @PassToken
    @GetMapping("/bottlecount")
    public CommonRetumType usertest(@RequestParam("openId") String openid)
    {
        List<Wxbottle> wxbottles = wxbottleserviceimpl.SelectcountOpenidkey(openid);
        int countopenid = 0;
        for (Wxbottle wxbottlecount: wxbottles) {
            countopenid = wxbottlecount.getBottlecount()+countopenid;
        }
        HashMap<String,Integer> hashMap = new HashMap<>();
        hashMap.put("bottlegain",userbottleserviceimpl.Selectbottlecount(openid));
        hashMap.put("bottlelost",userbottleserviceimpl.Selectbottlelostcount(openid));
        hashMap.put("bottleup",countopenid);

        return CommonRetumType.create(hashMap,"数量查询成功");
    }

    @PassToken
    @GetMapping("/create")
    public CommonRetumType create(Userbottle userbottle)
    {
        return CommonRetumType.create(userbottleserviceimpl.insertSelective(userbottle),"创建成功");
    }

//    获取捞过的瓶子
    @PassToken
    @GetMapping("/read")
    public CommonRetumType read(@RequestParam("openId") String openid)
    {
        List<Userbottle> userbottle = userbottleserviceimpl.SelectOpenidkey(openid);
        HashSet<Object> hashSet = new HashSet<Object>();
        if (userbottle == null){
            return CommonRetumType.create(null,"无绑定数据");
        }
        for (Userbottle bottleid: userbottle)
        {
            Wxbottle wxbottle1=wxbottleserviceimpl.selectByid(bottleid.getBottleid());
            Wxuser wxuser1 = wxuserMapper.selectByPrimaryKey(wxbottle1.getOpenId());
            wxbottle1.setWxuser(wxuser1);
            hashSet.add(wxbottle1);
        }

        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("openid",openid);
        hashMap.put("bottle",hashSet);
        return CommonRetumType.create(hashMap,"列表获取成功");
    }

    @PassToken
    @GetMapping("/delete")
    public CommonRetumType delete(@RequestParam("openId") String openid,
                                  @RequestParam("bottleid") Integer bottleid)
    {
        if (openid.equals(null))
        {
            return CommonRetumType.create("","openid不能为空");
        }
        if (bottleid.equals(null)){
            return CommonRetumType.create("","bottleid不能为空");
        }
        List<Userbottle> userbottle = userbottleserviceimpl.Selectindex(openid, bottleid);
        if (userbottle.equals(null)){
            return CommonRetumType.create("","userbottle删除对象不能存在数据库中");
        }
        for (Userbottle userbottleid: userbottle) {
            userbottleserviceimpl.Deletebottle(userbottleid.getId());
        }
        return CommonRetumType.create(1,"删除成功");

    }

}
